/*
 * Licensed to the Tai Ping Jin Ke
 *
 * Copyright (c) 2022 .
 * All rights reserved.
 * 项目名称：呼叫中心-业务逻辑层-任务调度-接口实现
 * 版权说明：本软件属太平金融科技服务(上海)有限公司所有，在未获得太平金融科技服务(上海)有限公司正式授权情况下，任何企业和个人，不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。
 */
package com.taiping.cc.job.service.impl;

import com.taiping.cc.application.constant.dao.ParamPositionEnum;
import com.taiping.cc.application.constant.service.PlatformServiceEnum;
import com.taiping.cc.job.dataobject.HttpParamConfigDO;
import com.taiping.cc.job.dataobject.HttpRequestConfigDO;
import com.taiping.cc.job.dataobject.HttpResponseConfigDO;
import com.taiping.cc.job.base.BaseService;
import com.taiping.cc.job.manager.common.HttpServiceCommon;
import com.taiping.cc.job.service.HttpService;
import com.taiping.cc.job.pojo.bo.ServiceConfigBO;
import com.taiping.cc.job.converter.ParamPropertyConverter;
import com.taiping.cc.job.converter.ServiceRequestConfigInfoConverter;
import com.taiping.cc.job.converter.ServiceResponseConfigInfoConverter;
import com.taiping.cc.job.pojo.query.ServiceConfigQuery;
import com.taipingframework.boot.web.response.result.ApiResult;
import com.taipingframework.boot.web.response.status.ApiStatusEnum;
import com.taipingframework.utility.http.ServiceConfigInfo;
import com.taipingframework.utility.http.ServiceConfigInfo.ServiceRequestConfigInfo;
import com.taipingframework.utility.http.ServiceConfigInfo.ServiceResponseConfigInfo;
import com.taipingframework.utility.http.ServiceConfigInfo.ParamProperty;
import com.taipingframework.utility.exception.ServiceException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Slf4j
@Service
@RequestMapping("/service/http")
public class HttpServiceImpl extends BaseService implements HttpService {

    private final HttpServiceCommon httpServiceCommon;

    @Override
    public ApiResult<Integer> saveHttpServiceConfig(ServiceConfigBO serviceConfigBO) throws ServiceException {
        return updateHttpServiceConfig(serviceConfigBO);
    }

    @Override
    public ApiResult<Integer> updateHttpServiceConfig(ServiceConfigBO serviceConfigBO) throws ServiceException {
        final String platformCode = serviceConfigBO.getPlatformCode();
        final String serviceCode = serviceConfigBO.getServiceCode();
        PlatformServiceEnum platformServiceEnum = PlatformServiceEnum.get(platformCode, serviceCode);
        ServiceConfigInfo serviceConfigInfo = serviceConfigBO.getServiceConfigInfo();

        try {
            httpServiceCommon.saveOrUpdateHttpServiceConfig(platformServiceEnum, serviceConfigInfo);
            // 返回响应结果
            return ApiResult.<Integer>build()
                    .setStatus(ApiStatusEnum.SUCCESS)
                    .setResult(1);
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public ApiResult<Integer> deleteHttpServiceConfig(ServiceConfigBO serviceConfigBO) throws ServiceException {
        final String platformCode = serviceConfigBO.getPlatformCode();
        final String serviceCode = serviceConfigBO.getServiceCode();
        PlatformServiceEnum platformServiceEnum = PlatformServiceEnum.get(platformCode, serviceCode);

        try {
            // 逻辑删除 服务请求配置
            httpRequestConfigDAO.deleteHttpRequestConfig(platformServiceEnum);

            // 逻辑删除 服务响应配置
            httpResponseConfigDAO.deleteHttpResponseConfig(platformServiceEnum);

            // 逻辑删除 服务参数配置
            httpParamConfigDAO.deleteHttpParamConfig(platformServiceEnum);

            // 返回响应结果
            return ApiResult.<Integer>build()
                    .setStatus(ApiStatusEnum.SUCCESS)
                    .setResult(1);
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public ApiResult<ServiceConfigInfo> queryHttpServiceConfig(ServiceConfigQuery serviceConfigQuery) throws ServiceException {
        final String platformCode = serviceConfigQuery.getPlatformCode();
        final String serviceCode = serviceConfigQuery.getServiceCode();
        PlatformServiceEnum platformServiceEnum = PlatformServiceEnum.get(platformCode, serviceCode);

        ServiceConfigInfo serviceConfigInfo = new ServiceConfigInfo();
        try {
            // 请求配置
            HttpRequestConfigDO httpRequestConfig = httpRequestConfigDAO.getHttpRequestConfigFromPlatform(platformServiceEnum);
            // 响应配置
            HttpResponseConfigDO httpResponseConfig = httpResponseConfigDAO.getHttpResponseConfigFromPlatform(platformServiceEnum);
            // 参数配置
            List<HttpParamConfigDO> httpParamConfigList = httpParamConfigDAO.getHttpParamConfigFromPlatform(platformServiceEnum);
            // 1 请求头参数配置
            List<ParamProperty> headerParamConfigList = httpParamConfigList.parallelStream()
                    .filter(t -> t.getServiceCode().equalsIgnoreCase(serviceCode)
                            && ParamPositionEnum.REQUEST_HEADER.getValue().equalsIgnoreCase(t.getParamPosition()))
                    .map(ParamPropertyConverter::convert)
                    .collect(Collectors.toList());
            // 2 URL参数配置
            List<ParamProperty> queryParamConfigList = httpParamConfigList.parallelStream()
                    .filter(t -> t.getServiceCode().equalsIgnoreCase(serviceCode)
                            && ParamPositionEnum.QUERY_PARAMS.getValue().equalsIgnoreCase(t.getParamPosition()))
                    .map(ParamPropertyConverter::convert)
                    .collect(Collectors.toList());
            // 3 FORM表单参数配置
            List<ParamProperty> formParamConfigList = httpParamConfigList.parallelStream()
                    .filter(t -> t.getServiceCode().equalsIgnoreCase(serviceCode)
                            && ParamPositionEnum.REQUEST_FORM.getValue().equalsIgnoreCase(t.getParamPosition()))
                    .map(ParamPropertyConverter::convert)
                    .collect(Collectors.toList());
            // 4 请求JSON报文占位符参数配置
            List<ParamProperty> reqJsonParamConfigList = httpParamConfigList.parallelStream()
                    .filter(t -> t.getServiceCode().equalsIgnoreCase(serviceCode)
                            && ParamPositionEnum.REQUEST_JSON.getValue().equalsIgnoreCase(t.getParamPosition()))
                    .map(ParamPropertyConverter::convert)
                    .collect(Collectors.toList());
            // 5 响应JSON报文字段映射配置
            List<ParamProperty> respJsonParamConfigList = httpParamConfigList.parallelStream()
                    .filter(t -> t.getServiceCode().equalsIgnoreCase(serviceCode)
                            && ParamPositionEnum.RESPONSE_JSON.getValue().equalsIgnoreCase(t.getParamPosition()))
                    .map(ParamPropertyConverter::convert)
                    .collect(Collectors.toList());

            // 6 网络服务请求参数配置
            ServiceRequestConfigInfo serviceRequestConfigInfo = ServiceRequestConfigInfoConverter.convert(httpRequestConfig);
            serviceRequestConfigInfo.setHeaderParamConfig(headerParamConfigList);
            serviceRequestConfigInfo.setQueryParamConfig(queryParamConfigList);
            serviceRequestConfigInfo.setFormParamConfig(formParamConfigList);
            serviceRequestConfigInfo.setJsonParamConfig(reqJsonParamConfigList);

            // 7 网络服务响应参数配置
            ServiceResponseConfigInfo serviceResponseConfigInfo = ServiceResponseConfigInfoConverter.convert(httpResponseConfig);
            serviceResponseConfigInfo.setMappingParamConfig(respJsonParamConfigList);

            // 8 网络服务配置信息
            serviceConfigInfo.setServiceRequestConfigInfo(serviceRequestConfigInfo);
            serviceConfigInfo.setServiceResponseConfigInfo(serviceResponseConfigInfo);

            // 9 返回网络服务配置信息
            return ApiResult.<ServiceConfigInfo>build()
                    .setStatus(ApiStatusEnum.SUCCESS)
                    .setResult(serviceConfigInfo);
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }
}
